﻿Public Class FrmKhachHang
    Private selectedmakh As String

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        PhucHoi()
        txtTenKH.Text = ""
        txtDiaChi.Text = ""
        txtDienThoai.Text = ""
        txtCMND.Text = ""
    End Sub

    Private Sub FrmKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvKH.DataSource = KhachHang.dskh()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        PhucHoi()
        If txtTenKH.Text <> "" And txtDiaChi.Text <> "" And txtDienThoai.Text <> "" And txtCMND.Text <> "" Then
            If Not KhachHang.them(txtTenKH.Text, txtDiaChi.Text, txtDienThoai.Text, txtCMND.Text) Then
                MsgBox("Không thể thêm dữ liệu!", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvKH.DataSource = KhachHang.dskh()
            btnNhapLai_Click(sender, e)
        Else
            MsgBox("Bạn cần nhập đầy đủ các trường thông tin vào", MsgBoxStyle.Information, "Thông báo")
        End If

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "&Sửa" Then
            selectedmakh = dgvKH.CurrentRow.Cells("MaKH").Value
            txtTenKH.Text = dgvKH.CurrentRow.Cells("TenKH").Value
            txtDiaChi.Text = dgvKH.CurrentRow.Cells("DiaChi").Value
            txtDienThoai.Text = dgvKH.CurrentRow.Cells("DienThoai").Value
            txtCMND.Text = dgvKH.CurrentRow.Cells("CMND").Value
            btnSua.Text = "&Lưu"
        Else
            If Not KhachHang.capnhat(selectedmakh, txtTenKH.Text, txtDiaChi.Text, txtDienThoai.Text, txtCMND.Text) Then
                MsgBox("Không thể lưu dữ liệu này", MsgBoxStyle.Critical, "Thông báo")
            End If
            dgvKH.DataSource = KhachHang.dskh()
            btnNhapLai_Click(sender, e)
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        PhucHoi()
        If MsgBox("Bạn chắc chắn muốn xóa khách hàng có tên " & dgvKH.CurrentRow.Cells("TenKH").Value & " không ? ", MsgBoxStyle.YesNo, "Thông báo") = DialogResult.Yes Then
            If Not KhachHang.xoa(dgvKH.CurrentRow.Cells("MaKH").Value) Then
                MsgBox("Bạn không thể xóa khách hàng trên", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvKH.DataSource = KhachHang.dskh()
            btnNhapLai_Click(sender, e)
        End If
     
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Hide()
        Me.Close()

    End Sub
    Private Sub PhucHoi()
        btnSua.Text = "&Sửa"
    End Sub
    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        PhucHoi()
        dgvKH.DataSource = KhachHang.timkiem(txtTuKhoa.Text)

    End Sub

    Private Sub btndaydu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndaydu.Click
        PhucHoi()
        dgvKH.DataSource = KhachHang.dskh()
    End Sub

    Private Sub dgvKH_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvKH.CellContentClick

    End Sub

    Private Sub dgvKH_CellContentDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvKH.CellContentDoubleClick
        PhucHoi()
        btnSua_Click(sender, e)
    End Sub
End Class